<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计算机案例</title>
</head>

<body>
    <div>
        <input type="text" size="5" id="num1">
        <select id="fh">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
            <option value="%">%</option>
        </select>
        <input type="text" size="5" id="num2">
        <input type="button" value="计算" id="btn">
    </div>
    <p class="cont">请输入运算的值</p>
    <script>
       
        document.getElementById("btn").onclick = function () {
            var count = "运算后的值：";
            var num1 = document.getElementById("num1").value;

            var num2 = document.getElementById("num2").value;

            var fh = document.getElementById("fh").value;
            var falg = true;
            if (num1 == "") {
                count += "输入值不能为空";
                falg = false;
            } else {
                num1 = parseInt(num1);
                if (isNaN(num1)) {
                    count += "输入的不是有效数字";
                    falg = false;
                }
            }
            if (num2 == "") {
                count += "输入值不能为空";
                falg = false;
            } else {
                num2 = parseInt(num2);
                if (isNaN(num2)) {
                    count = "输入的不是有效数字";
                    falg = false;
                }
            }
            if (falg) {
                switch (fh) {
                    case '+':
                        count = "运算后的结果"+(num1 + num2);
                        break;
                    case '-':
                        count ="运算后的结果"+( num1 - num2);
                        break;
                    case '*':
                        count ="运算后的结果"+( num1 * num2);
                        break;
                    case '/':
                        if (num2 == 0) {
                            count = "除数不能为零";
                        } else {
                            count ="运算后的结果"+( num1 / num2);
                        }
                        break;
                    case '%':
                        if (num2 == 0) {
                            count = "除数不能为零";
                        } else {
                            count ="运算后的结果"+( num1 % num2);
                        }

                        break;
                }
            }
            document.getElementsByClassName("cont")[0].innerHTML =count;

        }
    </script>
</body>

</html>